home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / content / overlay.js < prev    next >
Text File  |  2009-08-18  |  21KB  |  556 lines

  1. var stylishOverlay = {
  2.     pageStyleMenu: null,
  3.     service: Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle),
  4.     styleMenuItemTemplate: null,
  5.  
  6.     //cached number of global styles
  7.     globalCount: null,
  8.  
  9.     init: function() {        stylishOverlay.STRINGS = document.getElementById("stylish-strings");
  10.         stylishOverlay.URL_STRINGS = document.getElementById("stylish-url-strings");
  11.  
  12.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch2);
  13.         if (prefService.getIntPref("extensions.stylish.firstRun") == 0) {
  14.             if (typeof openUILinkIn != "undefined") {
  15.                 setTimeout(function() {openUILinkIn(stylishOverlay.URL_STRINGS.getString("firstrun"), "tab")}, 100);
  16.             }
  17.             prefService.setIntPref("extensions.stylish.firstRun", 1);
  18.         }
  19.  
  20.         stylishOverlay.styleMenuItemTemplate = document.createElementNS(stylishCommon.XULNS, "menuitem");
  21.         stylishCommon.domApplyAttributes(stylishOverlay.styleMenuItemTemplate, {
  22.             "type": "checkbox",
  23.             "class": "style-menu-item",
  24.             "oncommand": "stylishOverlay.toggleStyle(this.stylishStyle);event.stopPropagation();",
  25.             "context": "stylish-style-context"
  26.         });
  27.         //page load listener
  28.         var appcontent = document.getElementById("appcontent"); // browser
  29.         if (!appcontent) {
  30.             appcontent = document.getElementById("frame_main_pane"); // songbird
  31.         }
  32.         if (appcontent) {
  33.             appcontent.addEventListener("DOMContentLoaded", stylishOverlay.onPageLoad, true);
  34.         }
  35.         //page style menu item adder
  36.         stylishOverlay.pageStyleMenu = document.getElementById("pageStyleMenu") || document.getElementById("menu_UseStyleSheet");
  37.         if (stylishOverlay.pageStyleMenu) {
  38.             stylishOverlay.pageStyleMenu.firstChild.addEventListener("popupshowing", stylishOverlay.popupShowing, false);
  39.             stylishOverlay.pageStyleMenu.firstChild.addEventListener("popuphiding", stylishOverlay.pageStylePopupHiding, false);
  40.         }
  41.  
  42.         // sets an attribute for 24-based hour of the day
  43.         function updateHour() {
  44.             document.documentElement.setAttribute("stylish-hour", (new Date()).getHours())
  45.         }
  46.         // once a minute
  47.         setInterval(updateHour, 1000 * 60);
  48.         // now
  49.         updateHour();
  50.  
  51.         // the ways the current url can change:
  52.         if (typeof gBrowser != "undefined") {
  53.             // document loads
  54.             gBrowser.addProgressListener(stylishOverlay.urlLoadedListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT); 
  55.             // tab changes
  56.             // already covered by location changes?
  57.             gBrowser.tabContainer.addEventListener("TabSelect", stylishOverlay.urlUpdated, false);
  58.             //document.addEventListener("TabOpen", function(){setTimeout(stylishOverlay.urlUpdated,10)}, false);
  59.         }
  60.         // on a new browser
  61.         stylishOverlay.urlUpdated();
  62.  
  63.         // app info for styling
  64.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
  65.         document.documentElement.setAttribute("stylish-platform", window.navigator.platform);
  66.         document.documentElement.setAttribute("stylish-application", appInfo.name);
  67.         document.documentElement.setAttribute("stylish-application-version", appInfo.version);
  68.  
  69.         // other things that can change the status:
  70.  
  71.         // global on/off pref
  72.         prefService.addObserver("extensions.stylish.styleRegistrationEnabled", stylishOverlay, false);
  73.  
  74.         // style add/delete
  75.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  76.         observerService.addObserver(stylishOverlay, "stylish-style-change", false);
  77.         observerService.addObserver(stylishOverlay, "stylish-style-delete", false);
  78.     },
  79.  
  80.     destroy: function() {
  81.         if (typeof gBrowser != "undefined") {
  82.             gBrowser.removeProgressListener(stylishOverlay.urlLoadedListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT); 
  83.         }
  84.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  85.         observerService.removeObserver(stylishOverlay, "stylish-style-change", false);
  86.         observerService.removeObserver(stylishOverlay, "stylish-style-delete", false);
  87.     },
  88.  
  89.     observe: function(subject, topic, data) {
  90.         //clear global count cache
  91.         stylishOverlay.globalCount = null;
  92.         stylishOverlay.updateStatus();
  93.     },
  94.  
  95.     urlLoadedListener: {
  96.         QueryInterface: function(aIID) {
  97.             if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  98.                 aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  99.                 aIID.equals(Components.interfaces.nsISupports))
  100.                 return this;
  101.             throw Components.results.NS_NOINTERFACE; 
  102.         },
  103.         onLocationChange: function(progress, request, uri) {
  104.             // only if it's the current tab
  105.             if (uri && uri.spec == content.document.location.href) {
  106.                 stylishOverlay.urlUpdated();
  107.             }
  108.         },
  109.         onStateChange: function() {},
  110.         onProgressChange: function() {},
  111.         onStatusChange: function() {},
  112.         onSecurityChange: function() {},
  113.         onLinkIconAvailable: function() {}
  114.     },
  115.  
  116.     // some of reasons this will be called overlap, so make sure we're not doing extra work
  117.     lastUrl: null,
  118.  
  119.     urlUpdated: function() {
  120.         if (stylishOverlay.lastUrl == content.location.href)
  121.             return;
  122.         stylishOverlay.lastUrl = content.location.href;
  123.         document.documentElement.setAttribute("stylish-url", content.location.href);
  124.         try {
  125.             if (content.document.domain)
  126.                 document.documentElement.setAttribute("stylish-domain", content.document.domain);
  127.             else
  128.                 document.documentElement.setAttribute("stylish-domain", "");
  129.         } catch (ex) {
  130.                 document.documentElement.setAttribute("stylish-domain", "");
  131.         }
  132.         stylishOverlay.updateStatus();
  133.     },
  134.  
  135.     updateStatus: function() {
  136.         function updateAttribute(value) {
  137.             ["stylish-panel", "stylish-toolbar-button"].forEach(function(id) {
  138.                 var e = document.getElementById(id);
  139.                 if (e) {
  140.                     e.setAttribute("styles-applied", value);
  141.                 }
  142.             });
  143.         }
  144.  
  145.         function updateTooltip(string) {
  146.             var tooltip = document.getElementById("stylish-tooltip").firstChild;
  147.             while (tooltip.hasChildNodes()) {
  148.                 tooltip.removeChild(tooltip.lastChild);
  149.             }
  150.             tooltip.appendChild(document.createTextNode(string));
  151.         }
  152.  
  153.         if (!Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch2).getBoolPref("extensions.stylish.styleRegistrationEnabled")) {
  154.             updateAttribute("styles-off");
  155.             updateTooltip(stylishOverlay.STRINGS.getString("tooltipStylesOff"));
  156.             return;
  157.         }
  158.  
  159.         function isEnabled(style) {
  160.             return style.enabled;
  161.         }
  162.  
  163.         var siteStyles = stylishOverlay.service.findForUrl(content.location.href, false, 0, {}).filter(isEnabled).length;
  164.  
  165.         if (stylishOverlay.globalCount == null)
  166.             stylishOverlay.globalCount = stylishOverlay.service.findByMeta("type", "global", 0, {}).filter(isEnabled).length;
  167.  
  168.         var attributeValues = [];
  169.         if (siteStyles)
  170.             attributeValues.push("site");
  171.         if (stylishOverlay.globalCount)
  172.             attributeValues.push("global");
  173.         updateAttribute(attributeValues.join(" "));
  174.  
  175.         updateTooltip(stylishOverlay.STRINGS.getFormattedString("tooltip", [siteStyles, stylishOverlay.globalCount]));
  176.     },
  177.  
  178.     toggleStyle: function(style) {
  179.         style.enabled = !style.enabled;
  180.         style.save();
  181.     },
  182.  
  183.     isAllowedToInstall: function(doc) {
  184.         //this can throw for some reason
  185.         try {
  186.             var domain = doc.domain;
  187.         } catch (ex) {
  188.             return false;
  189.         }
  190.         if (!domain) {
  191.             return false;
  192.         }
  193.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  194.         prefs = prefs.getBranch("extensions.stylish.install.");
  195.         var allowedDomains = prefs.getCharPref("allowedDomains").split(" ");
  196.         if (allowedDomains.indexOf(doc.domain) > -1) {
  197.             return true;
  198.         }
  199.         //maybe this is a subdomain 
  200.         for (var i = 0; i < allowedDomains.length; i++) {
  201.             var subdomain = "." + allowedDomains[i];
  202.             var subdomainIndex = doc.domain.lastIndexOf(subdomain);
  203.             if (subdomainIndex > -1 && subdomainIndex == doc.domain.length - subdomain.length) {
  204.                 return true;
  205.             }
  206.         }
  207.         return false;
  208.     },
  209.  
  210.     getCodeFromPage: function(doc) {
  211.         //workaround for bug 194231 
  212.         var codeTextNodes = doc.getElementById("stylish-code").childNodes;
  213.         var code = ""
  214.         for (var i = 0; i < codeTextNodes.length; i++) {
  215.             code += codeTextNodes[i].nodeValue;
  216.         }
  217.         return code;
  218.     },
  219.  
  220.     checkUpdateEvent: function(doc, style) {
  221.         var code = stylishOverlay.getCodeFromPage(doc);
  222.         if (!stylishCommon.cssAreEqual((style.originalCode || style.code), code)) {
  223.             stylishCommon.dispatchEvent(doc, "styleCanBeUpdated");
  224.             doc.addEventListener("stylishUpdate", stylishOverlay.updateFromSite, false);
  225.         } else {
  226.             stylishCommon.dispatchEvent(doc, "styleAlreadyInstalled");
  227.         }
  228.     },
  229.  
  230.     onPageLoad: function(event) {
  231.         if (event.originalTarget.nodeName == "#document" && stylishOverlay.isAllowedToInstall(event.originalTarget)) {
  232.             var doc = event.originalTarget;
  233.  
  234.             //style installed status
  235.             var style = stylishOverlay.service.findByUrl(stylishCommon.cleanURI(doc.location.href), 0);
  236.             if (style) {
  237.                 //if the code isn't available, ask for it and wait
  238.                 var code = stylishOverlay.getCodeFromPage(doc);
  239.                 if (code) {
  240.                     stylishOverlay.checkUpdateEvent(doc, style);
  241.                 } else {
  242.                     doc.addEventListener("stylishCodeLoaded", function(){stylishOverlay.checkUpdateEvent(doc, style)}, false);
  243.                     stylishCommon.dispatchEvent(doc, "styleLoadCode");
  244.                 }
  245.             } else {
  246.                 stylishCommon.dispatchEvent(doc, "styleCanBeInstalled");
  247.                 doc.addEventListener("stylishInstall", stylishOverlay.installFromSite, false);
  248.             }
  249.         }
  250.     },
  251.  
  252.     installFromSite: function(event) {
  253.         var doc;
  254.         if (event.target.nodeName == "#document") {
  255.             doc = event.target;
  256.         }
  257.         var uri = stylishCommon.cleanURI(doc.location.href);
  258.         var links = doc.getElementsByTagName("link");
  259.         var code = null;
  260.         var description = null;
  261.         var updateURL = null;
  262.         var md5URL = null;
  263.         var installPingURL = null;
  264.         var triggeringDocument = null;
  265.         for (i in links) {
  266.             switch (links[i].rel) {
  267.                 case "stylish-code":
  268.                     var id = links[i].getAttribute("href").replace("#", "");
  269.                     var element = doc.getElementById(id);
  270.                     if (element) {
  271.                         code = element.textContent;
  272.                     }
  273.                     break;
  274.                 case "stylish-description":
  275.                     var id = links[i].getAttribute("href").replace("#", "");
  276.                     var element = doc.getElementById(id);
  277.                     if (element) {
  278.                         description = element.textContent;
  279.                     }
  280.                     break;
  281.                 case "stylish-install-ping-url":
  282.                     installPingURL = links[i].href;
  283.                     break;
  284.                 case "stylish-update-url":
  285.                     updateURL = links[i].href;
  286.                     break;
  287.                 case "stylish-md5-url":
  288.                     md5URL = links[i].href;
  289.                     break;                
  290.             }
  291.         }
  292.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  293.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  294.         style.init(uri, updateURL, md5URL, description, code, false, code);
  295.         stylishCommon.openInstall({style: style, installPingURL: installPingURL, triggeringDocument: doc});
  296.     },
  297.  
  298.     updateFromSite: function(event) {
  299.         var doc = event.target;
  300.         var uri = stylishCommon.cleanURI(doc.location.href);
  301.         style = stylishOverlay.service.findByUrl(uri, stylishOverlay.service.REGISTER_STYLE_ON_CHANGE + stylishOverlay.service.CALCULATE_META);
  302.         if (!style) {
  303.             return;
  304.         }
  305.         var links = doc.getElementsByTagName("link");
  306.         var code;
  307.         for (i in links) {
  308.             switch (links[i].rel) {
  309.                 case "stylish-code":
  310.                     var id = links[i].getAttribute("href").replace("#", "");
  311.                     var element = doc.getElementById(id);
  312.                     if (element) {
  313.                         code = element.textContent;
  314.                     }
  315.                     break;
  316.             }
  317.         }
  318.         if (!code) {
  319.             return;
  320.         }
  321.         var prompt = stylishOverlay.STRINGS.getFormattedString("updatestyle", [style.name])
  322.         var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  323.         if (prompts.confirmEx(window, stylishOverlay.STRINGS.getString("updatestyletitle"), prompt, prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_CANCEL, stylishOverlay.STRINGS.getString("updatestyleok"), null, null, null, {}) == 0) {
  324.             style.code = code;
  325.             //we're now in sync with the remote style
  326.             style.originalCode = code;
  327.             style.save();
  328.             stylishCommon.dispatchEvent(doc, "styleInstalled");
  329.         }
  330.     },
  331.  
  332.     installFromFile: function(event) {
  333.         var doc = content.document;
  334.         var uri = stylishCommon.cleanURI(doc.location.href);
  335.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  336.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  337.         style.init(uri, uri, null, null, doc.body.textContent, false, doc.body.textContent);
  338.         stylishCommon.openInstall({style: style, triggeringDocument: doc});
  339.     },
  340.  
  341.     writeStylePopupShowing: function(event) {
  342.         var popup = event.target;
  343.         var addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  344.         addSite.setAttribute("label", stylishOverlay.STRINGS.getString("writeforsite"));
  345.         addSite.setAttribute("accesskey", stylishOverlay.STRINGS.getString("writeforsiteaccesskey"));
  346.         addSite.setAttribute("oncommand", "stylishOverlay.addSite()");
  347.         popup.appendChild(addSite);
  348.  
  349.         var domain = null;
  350.         try {
  351.             domain = content.document.domain;
  352.         } catch (ex) {}
  353.         if (domain) {
  354.             var domains = [];
  355.             stylishOverlay.getDomainList(content.document.domain, domains);
  356.             for (var i = 0; i < domains.length; i++) {
  357.                 popup.appendChild(stylishOverlay.getDomainMenuItem(domains[i]));
  358.             }
  359.         }
  360.  
  361.         addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  362.         addSite.setAttribute("label", stylishOverlay.STRINGS.getString("writeblank"));
  363.         addSite.setAttribute("accesskey", stylishOverlay.STRINGS.getString("writeblankaccesskey"));
  364.         addSite.setAttribute("oncommand", "stylishOverlay.addCode('')");
  365.         popup.appendChild(addSite);
  366.     },
  367.  
  368.     popupShowing: function(event) {
  369.         var popup = event.target;
  370.         //this fires for its children too, but we don't want to do anything
  371.         if ((stylishOverlay.pageStyleMenu != null && popup != stylishOverlay.pageStyleMenu.firstChild) && popup.id != "stylish-popup") {
  372.             return;
  373.         }
  374.  
  375.         //popup.position = document.popupNode.nodeName == "toolbarbutton" ? "after_start" : "";
  376.  
  377.         //XXX fix for non-browsers (maybe list everything?)
  378.         var menuitems = stylishOverlay.service.findForUrl(content.location.href, stylishOverlay.service.REGISTER_STYLE_ON_CHANGE, true, {}).map(function(style, index) {
  379.             var menuitem = stylishOverlay.styleMenuItemTemplate.cloneNode(true);
  380.             stylishCommon.domApplyAttributes(menuitem, {
  381.                 "label": style.name,
  382.                 "checked": style.enabled,
  383.                 "style-type": style.getTypes({}).join(" ")
  384.             });        
  385.             if (index < 9) {
  386.                 menuitem.setAttribute("accesskey", index + 1);
  387.             }
  388.             menuitem.stylishStyle = style;
  389.             return menuitem;
  390.         });
  391.         if (menuitems.length > 0) {
  392.             var separator = document.createElementNS(stylishCommon.XULNS, "menuseparator");
  393.             separator.className = "stylish-menuseparator";
  394.             popup.appendChild(separator);
  395.         }
  396.         menuitems.forEach(function(menuitem) {
  397.             popup.appendChild(menuitem);
  398.         });
  399.  
  400.         //you can only add CSS files
  401.         document.getElementById("stylish-add-file").style.display = (content.document.contentType == "text/css") ? "-moz-box" : "none";
  402.         var stylesOn = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBoolPref("extensions.stylish.styleRegistrationEnabled");
  403.         document.getElementById("stylish-turn-on").style.display = stylesOn ? "none" : "-moz-box";
  404.         document.getElementById("stylish-turn-off").style.display = stylesOn ? "-moz-box" : "none";
  405.     },
  406.  
  407.     pageStylePopupHiding: function(event) {
  408.         var popup = event.target;
  409.         //this fires for its children too, but we don't want to do anything
  410.         if (popup != stylishOverlay.pageStyleMenu) {
  411.             return;
  412.         }
  413.         //wipe out the stuff we added
  414.         var separator = document.getElementById("stylishPageStyleSeparator");
  415.         while (separator.nextSibling) {
  416.             separator.parentNode.removeChild(separator.nextSibling);
  417.         }
  418.         separator.parentNode.removeChild(separator);
  419.     },
  420.  
  421.     getDomainList: function(domain, array) {
  422.         //don't want to list tlds
  423.         if (Components.interfaces.nsIEffectiveTLDService) {
  424.             var tld = Components.classes["@mozilla.org/network/effective-tld-service;1"].getService(Components.interfaces.nsIEffectiveTLDService);
  425.             if (Components.ID('{b07cb0f0-3394-572e-6260-dbaed0a292ba}').equals(Components.interfaces.nsIStyleSheetService)) {    
  426.                 if (domain.length <= tld.getEffectiveTLDLength(domain)) {
  427.                     return;
  428.                 }
  429.             } else {
  430.                 if (domain == tld.getPublicSuffixFromHost(domain)) {
  431.                     return;
  432.                 }
  433.             }
  434.         }
  435.         array[array.length] = domain;
  436.         var firstDot = domain.indexOf(".");
  437.         var lastDot = domain.lastIndexOf(".");
  438.         if (firstDot != lastDot) {
  439.             //if after the last dot it's a number, this is an ip address, so it's not part of a domain
  440.             if (!isNaN(parseInt(domain.substring(lastDot + 1, domain.length)))) {
  441.                 return;
  442.             }
  443.             stylishOverlay.getDomainList(domain.substring(firstDot + 1, domain.length), array);
  444.         }
  445.     },
  446.  
  447.     getDomainMenuItem: function(domain) {
  448.         var addSite = document.createElementNS(stylishCommon.XULNS, "menuitem");
  449.         addSite.setAttribute("label", stylishOverlay.STRINGS.getFormattedString("writefordomain", [domain]));
  450.         addSite.setAttribute("oncommand", "stylishOverlay.addDomain(\"" + domain + "\")");
  451.         return addSite;
  452.     },
  453.  
  454.     findStyle: function(e) {
  455.         openUILinkIn(stylishOverlay.URL_STRINGS.getFormattedString("findstylesforthissiteurl", [encodeURIComponent(content.location.href)]), "tab");
  456.     },
  457.  
  458.     clearStyleMenuItems: function(event) {
  459.         var popup = event.target;
  460.         for (var i = popup.childNodes.length - 1; i >= 0; i--) {
  461.             if (["stylish-menuseparator", "style-menu-item", "no-style-menu-item"].indexOf(popup.childNodes[i].className) >= 0) {
  462.                 popup.removeChild(popup.childNodes[i]);
  463.             }
  464.         }
  465.     },
  466.  
  467.     addSite: function() {
  468.         var url = content.location.href;
  469.         var code = "@namespace url(http://www.w3.org/1999/xhtml);\n\n@-moz-document url(\"" + url + "\") {\n\n}";
  470.         stylishOverlay.addCode(code);
  471.     },
  472.  
  473.     addDomain: function(domain) {
  474.         var code = "@namespace url(http://www.w3.org/1999/xhtml);\n\n@-moz-document domain(\"" + domain + "\") {\n\n}";
  475.         stylishOverlay.addCode(code);
  476.     },
  477.  
  478.     addCode: function(code) {
  479.         var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  480.         style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
  481.         style.init(null, null, null, null, code, false, null);
  482.         stylishCommon.openEdit(stylishCommon.getWindowName("stylishEdit"), {style: style});
  483.     },
  484.  
  485.     openManage: function() {
  486.         var manageView = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getIntPref("extensions.stylish.manageView");
  487.         function getWindow(name) {
  488.             return Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow(name);
  489.         }
  490.         switch (manageView) {
  491.             case 2: // sidebar
  492.                 var em = getWindow("navigator:browser");
  493.                 if (em) {
  494.                     em.toggleSidebar('viewStylishSidebar', true);
  495.                     break;
  496.                 }
  497.             case 1: // stand-alone dialog
  498.                 var em = getWindow("stylishManage");
  499.                 if (em) {
  500.                     em.focus();
  501.                 } else {
  502.                     window.openDialog("chrome://stylish/content/manage-standalone.xul", "", "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable");
  503.                 }
  504.                 break;
  505.             default: // add-ons
  506.                 var em = getWindow("Extension:Manager");
  507.                 if (em) {
  508.                     em.document.getElementById("userstyles-view").click();
  509.                     em.focus();
  510.                     return;
  511.                 }
  512.                 window.openDialog("chrome://mozapps/content/extensions/extensions.xul", "", "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable", "userstyles");
  513.         }
  514.     },
  515.  
  516.     showApplicableContextItems: function(event) {
  517.         var style = document.popupNode.stylishStyle;
  518.         document.getElementById("stylish-style-context-enable").hidden = style.enabled;
  519.         document.getElementById("stylish-style-context-disable").hidden = !style.enabled;
  520.     },
  521.  
  522.     contextSetEnabled: function(enabled) {
  523.         var style = document.popupNode.stylishStyle;
  524.         style.enabled = enabled;
  525.         style.save();
  526.     },
  527.  
  528.     contextEdit: function() {
  529.         stylishCommon.openEditForStyle(document.popupNode.stylishStyle);
  530.     },
  531.  
  532.     contextDelete: function() {
  533.         stylishCommon.deleteWithPrompt(document.popupNode.stylishStyle);
  534.     },
  535.  
  536.     turnOnOff: function(on) {
  537.         Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).setBoolPref("extensions.stylish.styleRegistrationEnabled", on);
  538.     },
  539.  
  540.     handleStatusClick: function(event) {
  541.         //show the menu on right-click
  542.         if (event.target.id == "stylish-panel") {
  543.             if (event.button == 2) {
  544.                 document.getElementById(event.target.getAttribute("popup")).openPopup(event.target, "before_start");
  545.             } else if (event.button == 1) {
  546.                 //open manage styles on middle click
  547.                 stylishOverlay.openManage();
  548.             }
  549.         }
  550.     },
  551. }
  552.  
  553. addEventListener("load", stylishOverlay.init, false);
  554. addEventListener("unload", stylishOverlay.destroy, false);
  555.  
  556.